// let base_url = process.env.NODE_ENV === 'development' ? 'http://bbsf.test/api/Wordsmp.V2.' :
// 	'https://px.aibaiba.com/api/Wordsmp.V2.';
// let base_url = process.env.NODE_ENV === 'development' ?  'http://bbsf.test/api/Wordsmp.V2.':process.env.VUE_APP_BASE_API+'/api/Wordsmp.V2.' 
// let base_url = process.env.NODE_ENV === 'development' ?  'https://px.aibaiba.com/api/Wordsmp.V2.':'https://px.aibaiba.com/api/Wordsmp.V2.' 
// let base_url = 'https://px.aibaiba.com/api/Wordsmp.V2.'
let base_url = 'https://bbsf.naomishu.cn/api/Wordsmp.V2.' //测试库
// let base_url = 'https://px.aibaiba.com/api/Wordsmp.V2.'
// let base_url = 'https://bbsf.naomishu.cn/api/Wordsmp.V2.' //测试库
// let isOver = true
// 封装请求方法
const request = ({
	url,
	method = 'POST',
	data
}) => {
	const token = uni.getStorageSync('token') || ''

	return new Promise((resolve, reject) => {
		// 不需要token的api
		let urls = {
			'Member/loginWordCode': '1',
			'WordMemExtend/getConfigSet': '1',
		}
		// return 到res层级的api
		let levels = {
			'Member/loginWordCode': '1',
			'Member/updateMobile': '1',
			'Member/updateUserInfo': '1',
			'Px.pxrecExt/add': '1',
			'Px.pxrec/singout': '1',
			'Px.pxrec/singin': '1',
			'Clue.student/upavatar': '1',
			'Px.pxrecnew/add': '1',
			'Admin.User/entry': '1',
			'Pb.attainment/add': '1',
			'Admin.User/getuseronlin': '1',
			'Px.pxrecnew/up_inspect': '1',
			'Student.student/add': '1',
			'Clue.contract/add': '1',
			'Finance.pay/add': '1',
			'Px.faq/detail': '1',
			"Px.faqExt/add": '1',
			'Px.faq/add': '1',
			// 'PracticeWords/index':'1',
			'PracticeWords/gostart': '1',
		}
		const param = {
			/*APP*/
			// #ifdef APP
			platform: 'APP',
			// #endif
			// #ifdef H5
			platform: 'H5',
			// #endif
			// // #ifdef MP-WEIXIN 
			// platform:'MP-WEIXIN',
			// // #endif
		}
		const header = {
			'Content-Type': 'application/json; charset=utf-8', // 设置请求头
		}
		if (!urls[url]) {
			let info = uni.getStorageSync('userInfo')
			// param['token'] = 'TERGFgNAOg8CE1tXUhtBQ0QDR1ZUVFYXC0c+RlRXUQRmRB9HC10HDwpUQ1xEBlABD1UDCgMIAwETSUBUBFoFB0VECQsTXglKREIVEwJSDUJoD1EaD1dGWV1JQFAOWRE9WQkRXwhHCQpKEwgCA1kXX0MfFwIXThEZEwQUUhVVE0ANRFsREkJfOkltTgQERAUYWBVGFVZXHkZZBAxUCVUITFYKWhwTXAYVSFIOCzoYAkZeOhoKBQsHBQg5TQFRBlVSDlUDVFAAU1ZfAVBSUwJXDhkMRV8XFRFYVAgAVhNHBBYVXEhHAUAEAgNuCAJEDVQaFQRaV15mWlETX0BjJGQmA1g8WwoIVTpXRB1DHw9ZBFReB1oaDwkfF0gQG1oPFltTG0RQDB5bCwFEC1FKRFoMRV8PFwIEFRFZVBMHX0MOUB8bREEAF0cAFRJuFQ8LUkEMBlEHAA0JBgMCV04RFVETD2gQUgkPVgwSHxNbXlADUwYHGw=='
			param['token'] = token
			param['user_id'] = info.user_id
			// param['user_id'] = 14
			param['dept_id'] = info.dept_id
			param['comp_no'] = info.comp_no
			param['role_id'] = info.role_id



			// header['token'] = token
			// header['user_id'] = info.user_id
			// header['dept_id'] = info.dept_id
			// header['comp_no'] = info.comp_no
			// header['role_id'] = info.role_id

		}

		Object.assign(param, data)
		uni.request({
			url: base_url + url,
			method: method,
			data: param,
			header: header,
			success: (res) => {
				if (res.data.event == 100 || res.data.status == 200) {
					// uni.hideLoading()
					if (levels[url]) {
						resolve(res);

					} else {
						resolve(res.data);
					}
				} else if (res.data.event == 0 || res.data.status == 0) {
					uni.hideLoading()
					uni.showToast({
						title: res.data.msg,
						icon: 'none'
					})
					resolve(res.data);
				} else if (res.data.event == 99 || res.data.status == 99) {
					uni.hideLoading()
					uni.reLaunch({
						url: '/pages/login/login'
					})
				} else if (res.data.status == 201) {
					uni.hideLoading()
					if (res.data.msg) {
						// uni.showToast({
						// 	title: res.data.msg,
						// 	icon: 'none'
						// })
					}
					resolve(res);
				} else {
					uni.hideLoading()
					uni.showToast({
						title: res.data.msg,
						icon: 'none'
					})
					resolve(res.data);
				}
			},
			fail: (err) => {
				uni.hideLoading()
				reject(err);
			},
		});
	});
}
const upload = ({
	url,
	method = 'POST',
	data
}) => {
	const token = uni.getStorageSync('token') || ''
	if (!token) {
		uni.showModal({
			title: '提示',
			content: '您还未登录！',
			cancelText: '关闭',
			confirmText: '去登录',
			success: res => {
				if (res.confirm) {
					uni.reLaunch({
						url: '/pages/login/login'
					})
				} else {
					uni.reLaunch({
						url: '/pages/index/index'
					})
				}
			}
		})
	}
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			url,
			header: {
				'token': token,
				// 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', // 设置请求头
			},
			name: 'file',
			formData: {
				file_: 'data',
				// type:'typetypetypetypetype'
			},
			success: (res) => {
				resolve(JSON.parse(res.data))
			}
		});
	})
}

// 配置请求拦截器
uni.addInterceptor('request', {
	// 在发送请求之前做一些处理
	config(requestConfig) {
		// 添加请求头、身份验证等
		// requestConfig.header.Authorization = 'Bearer ' + uni.getStorageSync('token');
		return requestConfig;
	},
	// 请求发生错误时的处理
	fail(error) {
		console.error('请求失败:', error);
	},
});
// type == 1 为普通请求 2为上传
const req = (url, data, method = 'POST', type = 1) => {
	return new Promise((r, v) => {
		if (type == 1) {
			request({
				url,
				method,
				data,

			}).then(res => {
				// console.log(res.data)
				r(res.data)
			})
		} else if (type == 2) {
			upload(base_url + 'Upload/upload', method, data).then(res => {
				r(res)
			})
		}
	})

}
export default req;